package org.example.service.impl;


import org.apache.tomcat.jni.Local;
import org.example.constant.MessageConstant;
import org.example.constant.NumConstant;
import org.example.constant.StatusConstant;
import org.example.domain.GoodsDeposit;
import org.example.domain.Grow;
import org.example.domain.Warehouse;
import org.example.dto.CropInHouseDto;
import org.example.dto.GoodsDto;
import org.example.dto.GrowDto;
import org.example.exception.LogGrowException;
import org.example.exception.PlantStatusException;
import org.example.mapper.CropMapper;
import org.example.mapper.GoodsDepositMapper;
import org.example.mapper.GrowMapper;
import org.example.mapper.WarehouseMapper;
import org.example.result.Result;
import org.example.service.GrowService;
import org.example.utils.InHouseUtil;
import org.example.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author SweetPig
 * @description 针对表【sys_grow】的数据库操作Service实现
 * @createDate 2024-05-28 21:47:04
 */
@Service
public class GrowServiceImpl implements GrowService {

    @Autowired
    private GrowMapper growMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CropMapper cropMapper;
    @Autowired
    private GoodsDepositMapper goodsDepositMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;

    /**
     * 农作物记录
     *
     * @param growLogDto
     */
    @Transactional
    public void logCrop(GrowDto growLogDto) {
        if (growLogDto.getStatus() != 0) {
            throw new LogGrowException(MessageConstant.LOG_GROW_ERROR);
        }
        String key = "grow_log_" + growLogDto.getCropId();
        Long increment = redisTemplate.opsForValue().increment(key);
        String id = new SnowFlake().nextId() + "";
        growLogDto.setId(id);
        assert increment != null;
        if (increment.intValue() > 2) {
            //记录三次
            redisTemplate.delete(key);
            //修改记录状态
            growLogDto.setStatus(StatusConstant.LOG_CROP_STATUS);
            cropMapper.updateStatus(growLogDto);
        }
        //设置记录农作物的时间
        growLogDto.setCreatedDate(LocalDateTime.now());
        //记录农作物
        growMapper.logCrop(growLogDto);
    }

    /**
     * 农作物记录列表
     *
     * @param id
     * @return
     */
    public List<Grow> growList(Integer id) {
        return growMapper.growList(id);
    }


    /**
     * 农作物入仓
     *
     * @param cropInHouseDto
     */
    @Transactional
    public void inHouse(CropInHouseDto cropInHouseDto) {
        //状态不为生长完成的状态无法运输
        if (!Objects.equals(cropInHouseDto.getStatus(), StatusConstant.PLANT_STATUS)) {
            throw new PlantStatusException(MessageConstant.CROP_STATUS_ERROR);
        }
        //获取分仓条件
        Integer growStatus = growMapper.getGrowStatus(cropInHouseDto);
        Integer houseId = InHouseUtil.returnHouse(growStatus);
        //查询仓库
        Warehouse warehouse = warehouseMapper.getById(houseId);
        //修改作物状态
        GrowDto growDto = GrowDto.builder().cropId(cropInHouseDto.getId()).status(StatusConstant.IN_HOUSE_STATUS).build();
        //复用修改状态代码
        cropMapper.updateStatus(growDto);
        //更新仓库容量
        GoodsDto build = GoodsDto.builder().id(houseId).capacity(warehouse.getCapacity() + NumConstant.CROP_DEFAULT_NUMBER).build();
        warehouseMapper.updateById(build);
        //入仓
        GoodsDeposit goodsDeposit = GoodsDeposit.builder().id(String.valueOf(new SnowFlake().nextId())).warehouseId(houseId).cropId(cropInHouseDto.getId()).warehousingNum(NumConstant.CROP_DEFAULT_NUMBER).warehousingTime(LocalDateTime.now()).build();
        goodsDepositMapper.saveDeposit(goodsDeposit);
    }
}




